import SwiftUI
import SwiftData
import Alamofire

// Login/userinfo
struct ResponseData_UserInfo: Codable {
    let status: Bool
    let description: String
    let nickName: String
    let avatarAddress: String
    let grade: Int
    let campus: String
    let college: String
    let contact: String
    let realNameStage: Int
    let realStudentNumber: String
    let realName: String
}

func RequestUserInfo(modelContext: ModelContext, userId: String) {
    let requestURL = "http://62.234.45.120:1230/Login/userinfo?userId=" + userId
    
    AF.request(requestURL, method: .get)
    .responseDecodable(of: ResponseData_UserInfo.self) { response in
        if let responseData = response.value {
            if responseData.status {
                let userInfo = UserInfo(nickName: responseData.nickName,
                                        avatarAddress: responseData.avatarAddress,
                                        grade: responseData.grade,
                                        campus: responseData.campus,
                                        college: responseData.college,
                                        contact: responseData.contact,
                                        realNameStage: responseData.realNameStage,
                                        realStudentNumber: responseData.realStudentNumber,
                                        realName: responseData.realName)
                modelContext.insert(userInfo)
            }
        }
    }
}

class LoginUserInfoFetcher: ObservableObject {
    @Published var publishedResult: UserInfo?
    
    func fetch(userId: String) {
        let requestURL = "http://62.234.45.120:1230/Login/userinfo?userId=" + userId
        
        AF.request(requestURL, method: .get)
        .responseDecodable(of: ResponseData_UserInfo.self) { response in
            if let responseData = response.value {
                if responseData.status {
                    self.publishedResult = UserInfo(nickName: responseData.nickName,
                                                    avatarAddress: responseData.avatarAddress,
                                                    grade: responseData.grade,
                                                    campus: responseData.campus,
                                                    college: responseData.college,
                                                    contact: responseData.contact,
                                                    realNameStage: responseData.realNameStage,
                                                    realStudentNumber: responseData.realStudentNumber,
                                                    realName: responseData.realName)
                }
            }
        }
    }
}

// Login/modifyuserinfo
struct RequestData_MoodifyUserInfo_basic: Codable {
    let userId: String
    let nickName: String
    let grade: Int
    let campus: String
    let college: String
    let contact: String
}

struct RequestData_MoodifyUserInfo_realname: Codable {
    let userId: String
    let realNameStage: Int
    let realStudentNumber: String
    let realName: String
}

struct ResponseData_MoodifyUserInfo: Codable {
    let status: Bool
    let description: String
}

class LoginMoodifyUserInfoRequester: ObservableObject {
    @Published var requestStatus: RequestStatus = .ready
    @Published var publishedResult: Bool = false
    
    func request_basic(userId: String, nickName: String, grade: String, campus: String, college: String, contact: String) {
        // 准备数据
        let requestData = RequestData_MoodifyUserInfo_basic(userId: userId,
                                                            nickName: nickName,
                                                            grade: Int(grade) ?? 0,
                                                            campus: campus,
                                                            college: college,
                                                            contact: contact)
        
        // 准备URL
        let requestURL = "http://62.234.45.120:1230/Login/modifyuserinfo"
        
        // 发起请求
        self.requestStatus = .requesting
        AF.request(requestURL,
                   method: .post,
                   parameters: requestData,
                   encoder: JSONParameterEncoder.default)
        .responseDecodable(of: ResponseData_MoodifyUserInfo.self) { response in
            if let responseData = response.value {
                if !responseData.status {
                    showMoodifyUserInfoAlert(description: responseData.description)
                }
                self.publishedResult = responseData.status
                self.requestStatus = .finish
            }
        }
    }
    
    func request_realname(userId: String, realNameStage: Int, realStudentNumber: String, realName: String) {
        // 准备数据
        let requestData = RequestData_MoodifyUserInfo_realname(userId: userId,
                                                               realNameStage: realNameStage,
                                                               realStudentNumber: realStudentNumber,
                                                               realName: realName)
        
        // 准备URL
        let requestURL = "http://62.234.45.120:1230/Login/modifyuserinfo"
        
        // 发起请求
        self.requestStatus = .requesting
        AF.request(requestURL,
                   method: .post,
                   parameters: requestData,
                   encoder: JSONParameterEncoder.default)
        .responseDecodable(of: ResponseData_MoodifyUserInfo.self) { response in
            if let responseData = response.value {
                if !responseData.status {
                    showMoodifyUserInfoAlert(description: responseData.description)
                }
                self.publishedResult = responseData.status
                self.requestStatus = .finish
            }
        }
    }
}
